package org.leumasjaffe.math.probability;

import java.util.Random;

import org.eclipse.jdt.annotation.NonNull;

public class GeometricRV extends DiscreteRV {
	private final double p;
	
	public GeometricRV(double p) {
		super();
		if (!isProb(p)) throw new IllegalArgumentException("Arguement is not a valid probability");
		this.p = p;
	}
	
	public GeometricRV(@NonNull Random r, double p) {
		super(r);
		if (!isProb(p)) throw new IllegalArgumentException("Arguement is not a valid probability");
		this.p = p;
	}

	@Override
	public double getEV() {
		return 1/this.p;
	}

	@Override
	public double getVar() {
		return (1-this.p)/(this.p*this.p);
	}

	@Override
	public double getP(int x) {
		return Math.pow(1-this.p, x-1)*this.p;
	}

	@Override
	public double calcX(double p) {
		if (!isProb(p)) throw new IllegalArgumentException("Arguement is not a valid probability");
		return (int) (Math.log10(1-p)/Math.log10(1-this.p));
	}

	@Override
	public double getF(int x) {
		return 1 - Math.pow(1-this.p, x);
	}

}
